package com.itmatter.classifier.listener;

import com.enigmastation.classifier.CategoryIncrement;
import com.enigmastation.classifier.ClassifierListener;
import com.enigmastation.classifier.FeatureIncrement;
import com.itmatter.classifier.util.ClassifierHibernateUtil;

import com.itmatter.classifier.domain.ClassifierCategory;
import com.itmatter.classifier.domain.ClassifierFeature;
import com.itmatter.classifier.domain.ClassifierFeatureId;

import org.apache.log4j.Logger;

import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Daniel Fisla, dfisla@itmatter.com, ITMATTER Inc., http://www.itmatter.com, Copyright 2010, LGPLv3
 */
public class DatabaseClassifierListener implements ClassifierListener {

    private static final Logger logger = Logger.getLogger(DatabaseClassifierListener.class.getName());

    /**
     * 
     */
    public DatabaseClassifierListener() {
    }

    /**
     * 
     * @param featureIncrement
     */
    public void handleFeatureUpdate(FeatureIncrement featureIncrement) {

        Transaction tx = null;
        try {
            Session session = ClassifierHibernateUtil.getSessionFactory().getCurrentSession();
            tx = session.beginTransaction();
            session.setFlushMode(FlushMode.COMMIT);

            ClassifierFeature cFeature = (ClassifierFeature) session.get(ClassifierFeature.class,
                    new ClassifierFeatureId(featureIncrement.getFeature(), featureIncrement.getCategory()));

            if (cFeature == null) {
                cFeature = new ClassifierFeature( new ClassifierFeatureId(featureIncrement.getFeature(), featureIncrement.getCategory()));
            }
            cFeature.setCounts(featureIncrement.getCount());
         
            session.saveOrUpdate(cFeature);
            tx.commit();
            logger.debug(String.format("ADD-CLASSIFIER-FEATURE: %s, %s, %d", cFeature.getId().getFeature(), cFeature.getId().getCategory(), cFeature.getCounts()));

        } catch (HibernateException eh) {
            try {
                if (tx != null) {
                    tx.rollback();
                    logger.error("ROLLBACK!");
                }
            } catch (HibernateException eh2) {
            }

            logger.error(eh.getMessage(), eh);
        }
    }

    /**
     * 
     * @param categoryIncrement
     */
    public void handleCategoryUpdate(CategoryIncrement categoryIncrement) {
        
        Transaction tx = null;
        try {
            Session session = ClassifierHibernateUtil.getSessionFactory().getCurrentSession();
            tx = session.beginTransaction();
            session.setFlushMode(FlushMode.COMMIT);

            ClassifierCategory cCategory = (ClassifierCategory) session.get(ClassifierCategory.class, categoryIncrement.getCategory());

            if (cCategory == null) {
                cCategory= new ClassifierCategory(categoryIncrement.getCategory());
            }
            cCategory.setCounts(categoryIncrement.getCount());
         
            session.saveOrUpdate(cCategory);
            tx.commit();
            logger.debug(String.format("ADD-CLASSIFIER-CATEGORY: %s, %d", cCategory.getCategory(), cCategory.getCounts()));

        } catch (HibernateException eh) {
            try {
                if (tx != null) {
                    tx.rollback();
                    logger.error("ROLLBACK!");
                }
            } catch (HibernateException eh2) {
            }

            logger.error(eh.getMessage(), eh);
        }
    }
}
